package com.hmy.contract.service.contract.save.supply.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.hmy.contract.dal.po.ContractPo;
import com.hmy.contract.service.dto.contract.FieldDto;
import com.hmy.contract.service.dto.contract.SupplyOrderDetailDto;
import com.hmy.contract.service.dto.contract.SupplyOrderDeviceDetailDto;
import com.hmy.contract.service.dto.save.SaveSupplyContractDto;
import com.hmy.order.api.common.enums.OrderDeviceEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description
 * @date 2024/3/11 19:02
 */
@Service
@Slf4j
public class SupplyContractFieldBuildService {

    public List<FieldDto> fillFieldValue(SaveSupplyContractDto dto, ContractPo contractPo) {
        List<FieldDto> fieldDtoList = new ArrayList<>();
        SupplyOrderDetailDto supplyOrderDetail = dto.getSupplyOrderDetail();
        fieldDtoList.add(FieldDto.builder().key("承租方1").value(supplyOrderDetail.getCustomerContactorName()).build());
        fieldDtoList.add(FieldDto.builder().key("年份").value(String.valueOf(DateUtil.format(contractPo.getCreateTime(), "YYYY"))).build());
        fieldDtoList.add(FieldDto.builder().key("月份").value(String.valueOf(DateUtil.format(contractPo.getCreateTime(), "MM"))).build());
        fieldDtoList.add(FieldDto.builder().key("日份").value(String.valueOf(DateUtil.format(contractPo.getCreateTime(), "dd"))).build());
        fieldDtoList.add(FieldDto.builder().key("合同编号").value(contractPo.getContractNo().substring(5)).build());
        fieldDtoList.add(FieldDto.builder().key("签约日期").value(String.valueOf(DateUtil.format(new Date(System.currentTimeMillis()), "YYYY年MM月dd日"))).build());
        List<SupplyOrderDeviceDetailDto> supplyOrderDeviceDetails = supplyOrderDetail.getSupplyOrderDeviceDetails();
        int index = 1;
        for (SupplyOrderDeviceDetailDto supplyOrderDeviceDetail : supplyOrderDeviceDetails) {
            fieldDtoList.add(FieldDto.builder().key("设备类型" + index).value(OrderDeviceEnum.getDescByType(supplyOrderDeviceDetail.getDeviceType())).build());
            fieldDtoList.add(FieldDto.builder().key("设备型号" + index).value(supplyOrderDeviceDetail.getDeviceModel()).build());
            fieldDtoList.add(FieldDto.builder().key("数量" + index).value(String.valueOf(supplyOrderDeviceDetail.getDeviceNum())).build());
            fieldDtoList.add(FieldDto.builder().key("预计交接日" + index).value(String.valueOf(DateUtil.format(supplyOrderDeviceDetail.getHandoverTime(), "YYYY/MM/dd"))).build());
            fieldDtoList.add(FieldDto.builder().key("租赁方式" + index).value(supplyOrderDeviceDetail.getRentalType() == 1 ? "日租" : "月租").build());
            fieldDtoList.add(FieldDto.builder().key("最短租期天" + index).value(String.valueOf(supplyOrderDeviceDetail.getShortestRentalPeriod())).build());
            fieldDtoList.add(FieldDto.builder().key("预计租期" + index).value(String.valueOf(supplyOrderDeviceDetail.getEstimatedRentalPeriod())).build());
            fieldDtoList.add(FieldDto.builder().key("日租单价" + index).value(String.valueOf(NumberUtil.roundStr(supplyOrderDeviceDetail.getDailyRent().toString(), 0))).build());
            fieldDtoList.add(FieldDto.builder().key("月租单价" + index).value(String.valueOf(NumberUtil.roundStr(supplyOrderDeviceDetail.getMonthlyRent().toString(), 0))).build());
            index++;
        }
        log.debug("field list : {}", JSONUtil.toJsonPrettyStr(fieldDtoList));
        return fieldDtoList;
    }

}
